package com.gcxy.springmybatisplustest.handler;

import com.gcxy.springmybatisplustest.domain.result.ApiResponse;
import com.gcxy.springmybatisplustest.exception.BusinessException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {BusinessException.class})
    public ApiResponse BusinessExceptionHandler(BusinessException e){
        return ApiResponse.error(e.getMessage());
    }


    @ExceptionHandler(value = {Exception.class})
    public ApiResponse ExceptionHandler(Exception e){
        return ApiResponse.error(e.getMessage());
    }

    @ExceptionHandler(value = {MethodArgumentNotValidException.class})
    public ApiResponse MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        return ApiResponse.error(e.getMessage());
    }
}
